using System;
using System.Drawing;
using System.ComponentModel;
using System.Collections.Generic;

namespace SCADAEditor.Component
{
    /// <summary>
    /// 状态指示灯组件类，继承自ComponentBase
    /// 功能：用于显示设备状态的指示灯或切换开关
    /// 特性：
    /// - 支持两种状态显示（激活/非激活）
    /// - 可配置设备类型和地址
    /// - 支持自定义状态图片
    /// - 可设置为指示灯模式或切换开关模式
    /// </summary>
    public class StatusIndicatorComponent : ComponentBase
    {
        private string _description = "状态指示灯";
        private bool _isIndicator = true;
        private DeviceType _deviceType = DeviceType.H3U;
        private string _addressType = "D";
        private string _address = "0";
        private bool _invertOutput = false;
        private string _state0Image = "";
        private string _state1Image = "";
        private bool _useStateImages = false;

        private bool _OnOFF = false; 

        [Browsable(false)]  // 添加这行使属性在属性窗口中不可见
        [Category("信号绑定")]
        [Description("外部信号控制灯的状态")]
        [DisplayName("开关状态")]  // 添加这行来指定UI显示名称
        public bool OnOff
        {
            get { return _OnOFF; }
            set { _OnOFF = value; OnPropertyChanged(); }
        }

          private string _onOffSignalName = "";

        [Category("信号绑定")]
        [Description("开关状态信号由外部信号控制,注意你起的名字必须唯一性")]
        [DisplayName("开关状态信号名称")]
        public string OnOffSignalName
        {
            get { return _onOffSignalName; }
            set { _onOffSignalName = value; OnPropertyChanged(); }
        }

        private bool _isActive = false;

        [Category("状态")]
        [Description("当前状态")]
        public bool IsActive
        {
            get { return _isActive; }
            set { _isActive = value; OnPropertyChanged(); }
        }
        
        [Category("基本设置")]
        [Description("组件描述")]
        public string Description
        {
            get { return _description; }
            set { _description = value; OnPropertyChanged(); }
        }
        
        [Category("功能设置")]
        [Description("是否为状态指示灯(否则为切换开关)")]
        public bool IsIndicator
        {
            get { return _isIndicator; }
            set { _isIndicator = value; OnPropertyChanged(); }
        }
        
        [Category("设备设置")]
        [Description("设备类型")]
        public DeviceType DeviceType
        {
            get { return _deviceType; }
            set { _deviceType = value; OnPropertyChanged(); }
        }
        
        [Category("设备设置")]
        [Description("地址类型")]
        public string AddressType
        {
            get { return _addressType; }
            set { _addressType = value; OnPropertyChanged(); }
        }
        
        [Category("设备设置")]
        [Description("设备地址")]
        public string Address
        {
            get { return _address; }
            set { _address = value; OnPropertyChanged(); }
        }
        
        [Category("设备设置")]
        [Description("是否输出反向")]
        public bool InvertOutput
        {
            get { return _invertOutput; }
            set { _invertOutput = value; OnPropertyChanged(); }
        }
        
        [Category("外观设置")]
        [Description("状态0图片")]
        public string State0Image
        {
            get { return _state0Image; }
            set { _state0Image = value; OnPropertyChanged(); }
        }
        
        [Category("外观设置")]
        [Description("状态1图片")]
        public string State1Image
        {
            get { return _state1Image; }
            set { _state1Image = value; OnPropertyChanged(); }
        }
        
        [Category("外观设置")]
        [Description("是否使用状态图片")]
        public bool UseStateImages
        {
            get { return _useStateImages; }
            set { _useStateImages = value; OnPropertyChanged(); }
        }

         public StatusIndicatorComponent()
        {
            Size = new Size(23, 23);
            Text = "statusLight";
            IsEditMode = true;
            
            // 现在PropertyChanged事件可以正常使用了
            this.PropertyChanged += (s, e) => 
            {
                if (e.PropertyName == nameof(OnOffSignalName) && !string.IsNullOrEmpty(OnOffSignalName))
                {
                    SignalService.Instance.RegisterSignalHandler(OnOffSignalName, value => OnOff = value);
                }
            };
        }
        

        public override void Draw(Graphics g)
        {
            // 这里需要实现绘制逻辑
            // 根据状态和图片设置绘制组件
            if (_useStateImages)
            {
                // 使用状态图片的逻辑
                // ... existing code ...
            }
            else
            {
                // 自绘状态灯
                const int size = 20;
                var rect = new Rectangle(Position.X, Position.Y, size, size);
                
                // 绘制边框
                using (var pen = new Pen(Color.Black, 2))
                {
                    g.DrawEllipse(pen, rect);
                }
                
                using (var brush = new SolidBrush(_isActive ? Color.Green : Color.Red))
                {
                    g.FillEllipse(brush, rect);
                }
            }
        }

        public override Dictionary<string, object> ToJson()
        {
            var dict = base.ToJson();
            dict.Add("Description", _description);
            dict.Add("IsIndicator", _isIndicator);
            dict.Add("DeviceType", (int)_deviceType);
            dict.Add("AddressType", _addressType);
            dict.Add("Address", _address);
            dict.Add("InvertOutput", _invertOutput);
            dict.Add("State0Image", _state0Image);
            dict.Add("State1Image", _state1Image);
            dict.Add("UseStateImages", _useStateImages);
            dict.Add("IsActive", _isActive);
            return dict;
        }

        public override void FromJson(Dictionary<string, object> json)
        {
            base.FromJson(json);
            _description = (string)json["Description"];
            _isIndicator = (bool)json["IsIndicator"];
            _deviceType = (DeviceType)(int)json["DeviceType"];
            _addressType = (string)json["AddressType"];
            _address = (string)json["Address"];
            _invertOutput = (bool)json["InvertOutput"];
            _state0Image = (string)json["State0Image"];
            _state1Image = (string)json["State1Image"];
            _useStateImages = (bool)json["UseStateImages"];
            _isActive = (bool)json["IsActive"];
        }
    }

    public enum DeviceType
    {
        H3U,
        H5U
    }
}